package com.benyi.byiee.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.benyi.byiee.constants.Status;
import com.benyi.byiee.dao.mappers.IndustryClassifyMapper;
import com.benyi.byiee.model.db.IndustryClassify;
import com.benyi.util.Config;
import com.benyi.util.bean.ServiceException;

import jakarta.annotation.Resource;

@Service
public class IndustryClassifyService {
	private static Logger log = LoggerFactory.getLogger(IndustryClassifyService.class);
	
	@Resource(name="config")
	private Config config; 
	
	@Autowired
	IndustryClassifyMapper classifyMapper;
	
	public List<IndustryClassify>  queryList(String code){
		if(code !=null && code.length()>=1) {
			code = code.substring(0,1)+"%";
		}else {
			code="%";
		} 
		List<IndustryClassify> list = classifyMapper.queryList(code);
		return list;
	}
	
	public IndustryClassify save(IndustryClassify v) throws ServiceException {
		String code = v.getYcode();
		if(code==null || code.length()==0) {
			throw new ServiceException("null_code","Code can not be null");
		} 
		if( v.getStatus()==null || !( v.getStatus() == Status.INVALID || v.getStatus() == Status.VALID) ) {
			v.setStatus(Status.INVALID);
		}
		try {
			IndustryClassify c = classifyMapper.selectByPrimaryKey(v.getYcode()); 
			if(c==null) {
				classifyMapper.insert(v);
			}else {
				classifyMapper.updateByPrimaryKey(v);
			} 
			c = classifyMapper.selectByPrimaryKey(v.getYcode());
			return c;
		}catch(Exception e) {
			log.info("Exception", e);
			throw new ServiceException("save-exception",e.getMessage());
		} 
	}
	
	public int delete(IndustryClassify v) throws ServiceException { 
		try { 
			int r = classifyMapper.deleteByPrimaryKey(v.getYcode());  
			return r; 
		}catch(Exception e) {
			log.info("Exception", e);
			throw new ServiceException("cannot_remove",e.getMessage());
		} 
	}
	
	public IndustryClassify selectByCode(String code) {  
		IndustryClassify c = classifyMapper.selectByPrimaryKey(code);  
		return c; 
	}
	
}
